import axios from 'axios';
import Qs from 'qs';
import { Toast } from 'vant'
const service = axios.create({
    baseURL: 'http://47.94.211.25:8080',
    timeout: 5000
});


service.interceptors.request.use(
    config => {
        config.transformRequest = [function (data) {
            // 在请求之前对data传参进行格式转换
            data = Qs.stringify(data);
            startLoading();
            return data;
        }]
        return config;
    },
    error => {
        console.log(error);
        return Promise.reject();
    }
);

service.interceptors.response.use(
    response => {
        if (response.status === 200) {
            endLoading();
            return response.data;
           
        } else {
            
            endLoading();
            Toast.fail({message:'加载失败',duration:1000});
            Promise.reject();
        }
    },
    error => {
       
        console.log(error);
        endLoading();
        Toast.fail({message:'加载失败',duration:1000});
        return Promise.reject();
    },
   
);
function startLoading() {
    // 开始加载
    Toast.loading({
        forbidClick: true,
    });
}
function endLoading() {
    // 结束加载
    Toast.clear()
}
export default service;
